package com.tedu._02notice.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu._02notice.base.response.JsonResult;
import com.tedu._02notice.base.response.StatusCode;
import com.tedu._02notice.mapper.NoticeMapper;
import com.tedu._02notice.pojo.dto.NoticeAddParam;
import com.tedu._02notice.pojo.dto.NoticeAddQuery;
import com.tedu._02notice.pojo.dto.NoticeUpdateParam;
import com.tedu._02notice.pojo.entity.Notice;
import com.tedu._02notice.pojo.vo.NoticeDetailInfoVo;
import com.tedu._02notice.pojo.vo.NoticeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//@Controller
@Slf4j
@RestController
@RequestMapping("/v1/notice")
@Tag(name= "1-资询管理" ,description ="1-资询管理" )
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("/add")
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
//    @ResponseBody
    public JsonResult insert(@RequestBody NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam = "+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setUserId(100l);

        int num = noticeMapper.insert(notice);
        System.out.println(num>0?"添加资讯成功":"添加失败");
        return JsonResult.success();
    }
    @GetMapping("/list")
    @Operation(summary = "查询资讯")
    @ApiOperationSupport(order = 20)
//    @ResponseBody
    public JsonResult select(NoticeAddQuery noticeAddQuery){
        List<NoticeVO> noticeVOList = noticeMapper.select(noticeAddQuery);
        return JsonResult.success(noticeVOList);
    }
    @GetMapping("/detail")
    @Operation(summary = "查询咨询详情")
    @ApiOperationSupport(order = 30)
    @Parameters({@Parameter(name="id",description = "咨询编号",required = true),
            @Parameter(name="title",description = "咨询标题",required = true,hidden = true)})
//    @ResponseBody
    public JsonResult selectDetail(Integer id,String title){
        NoticeDetailInfoVo noticeDetailInfoVo = noticeMapper.selectDetail(id);
        return JsonResult.success(noticeDetailInfoVo);
    }
    @PostMapping("/update")
    @Operation(summary = "更新资讯")
    @ApiOperationSupport(order = 40)
//    @ResponseBody
    @Parameters({
            @Parameter(name = "id"),
            @Parameter(name = "title"),
            @Parameter(name = "content"),
            @Parameter(name = "type"),
            @Parameter(name = "status"),
            @Parameter(name = "updateParam",hidden = true),

    })
    public JsonResult update(NoticeUpdateParam updateParam){
        Notice notice = new Notice();
        BeanUtils.copyProperties(updateParam,notice);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.update(notice);
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @DeleteMapping ("/del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 60)
//    @ResponseBody
    public JsonResult delete(Integer id){
        int num = noticeMapper.delete(id);
        return num>0?JsonResult.success():new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
